﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MapData;
using ExportData;
using ImportData;
using System.Threading;


namespace KMLConverter {
    public partial class Form1 : Form {


        private string filter;
        private Thread thread;
        private int pValue;
        

        public Form1() {
            InitializeComponent();
            
            
        }

        private void Form1_Load(object sender, EventArgs e) {
//            try
//            {
//                string testFile = "QLD_Federal_Electoral_Boundaries.MIF";
//                string xlsfile = "Qld_FederalResults by Electorate-2004.xls";

//                ImportMapDocument importer = new ImportMIF();
//                MapDocument doc = importer.Load(testFile);

////                importer = new ImportXLS();
////                doc = importer.Append(xlsfile, doc);

//                ExportKML exporter = new ExportData.ExportKML();
//                string kmlFileName = "TestFile.kml";
//                exporter.Save(kmlFileName, doc);
//                System.Diagnostics.Process.Start(kmlFileName);
//                Application.Exit();
//            }
//            catch (Exception ex)
//            {
//                MessageBox.Show(ex.ToString());
//            }
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
        

                //Thread threadInstance = new Thread(startConversion);
                //isThreadRunning = true;
                //threadInstance.Start(); 
                //lblMessage.Text = "Conversion in progress. This may take several seconds";  
                btnConvert.Enabled = false;
                btnConvert.Text = "Converting";
                progressBar1.Visible = true;
                label3.Visible = false;
                progressBar1.Value = 0;
                filter = (string)lstRegions.SelectedItem;
                thread = new Thread(startConversion);
                thread.Start();
                //lblMessage.Text = "Conversion Complete.";

            
        }


        private void startConversion() {           
            try
            {
                string testFile = "QLD_Federal_Electoral_Boundaries.MIF";
                string accessFile = "ElectoralData.mdb";
                //string xlsfile = "Qld_FederalResults by Electorate-2004.xls";
                pValue = 60;
                this.Invoke(new UpdateProgressDelegate(UpdateProgress));
                ImportMapDocument importer;
                ImportMIF mifImport = new ImportMIF();

                if (filter == "Northern Queensland") {
                    mifImport.addFilter("Leichhardt");
                    mifImport.addFilter("Kennedy");
                    mifImport.addFilter("Herbert");
                    mifImport.addFilter("Dawson");
                } else if (filter == "South Eastern Queensland") {
                    mifImport.addFilter("Mcpherson");
                    mifImport.addFilter("Forde");
                    mifImport.addFilter("Moncrieff");
                    mifImport.addFilter("Fadden");
                } else if (filter == "Brisbane") {
                    mifImport.addFilter("Bowman");
                    mifImport.addFilter("Bonner");
                    mifImport.addFilter("Oxley");
                    mifImport.addFilter("Rankin");
                    mifImport.addFilter("Moreton");
                    mifImport.addFilter("Ryan");
                    mifImport.addFilter("Brisbane");
                    mifImport.addFilter("Griffith");
                     mifImport.addFilter("Lilley");
                    mifImport.addFilter("Dickson");
                    mifImport.addFilter("Petrie");
                    mifImport.addFilter("Longman");
                } else if (filter == "Western Queensland") {
                    mifImport.addFilter("Kennedy");
                    mifImport.addFilter("Maranoa");
                } else if (filter == "Eastern Queensland") {
                    mifImport.addFilter("Longman");
                    mifImport.addFilter("Fisher");
                    mifImport.addFilter("Fairfax");
                    mifImport.addFilter("Wide Bay");
                    mifImport.addFilter("Hinkler");
                    mifImport.addFilter("Capricornia");
                    mifImport.addFilter("Dawson");
                    mifImport.addFilter("Longman");
                    mifImport.addFilter("Herbert");
                }

                importer = mifImport;
                
                MapDocument doc = importer.Load(testFile);
                importer = new ImportAccess();

                doc = importer.Append(accessFile, doc);
                /*
                if (cbImportXls.Checked)
                {
                    importer = new ImportXLS();
                    doc = importer.Append(xlsfile, doc);
                }*/
                pValue = 80;
                this.Invoke(new UpdateProgressDelegate(UpdateProgress));
                ExportKML exporter = new ExportData.ExportKML();
                string kmlFileName = "TestFile.kml";
                pValue = 100;
                this.Invoke(new UpdateProgressDelegate(UpdateProgress));
                exporter.Save(kmlFileName, doc);
                this.Invoke(new CompleteDelegate(Complete));
                //System.Diagnostics.Process.Start(kmlFileName);
                //Application.Exit();
                //lblMessage.Text = "Conversion Complete.";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                //lblMessage.Text = "Conversion failed";
            }

        }
        delegate void UpdateProgressDelegate();

        private void UpdateProgress() {
         progressBar1.Value = pValue;
        }

        delegate void CompleteDelegate();

        private void Complete() {
            progressBar1.Visible = false;
            label3.Visible = true;
            btnConvert.Enabled = true;
            btnConvert.Text = "Convert";
        }
        private void label1_Click(object sender, EventArgs e) {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {

        }
        
    }
}
